package com.ruoyi.carbon.service.impl;

import com.ruoyi.carbon.domain.vo.RukuShenqingVo;
import com.ruoyi.carbon.entity.RukuShenqing;
import com.ruoyi.carbon.entity.RukuShenqingDetail;
import com.ruoyi.carbon.mapper.RukuShenqingDetailMapper;
import com.ruoyi.carbon.mapper.RukuShenqingMapper;
import com.ruoyi.carbon.service.RukuShenqingService;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class RukuShenqingServiceImpl implements RukuShenqingService {

    @Autowired
    private RukuShenqingMapper rukuShenqingMapper;

    @Autowired
    private RukuShenqingDetailMapper rukuShenqingDetailMapper;

    @Override
    public List<RukuShenqing> list(RukuShenqing rukuShenqing) {
        return rukuShenqingMapper.list(rukuShenqing);
    }

    @Override
    @Transactional
    public int remove(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new ServiceException("请选择要删除的数据");
        }
        rukuShenqingDetailMapper.delByRkId(ids);
        return rukuShenqingMapper.remove(ids);
    }

    @Override
    public RukuShenqingVo getById(Long id) {

        RukuShenqing rukuShenqing = rukuShenqingMapper.getById(id);
        if (ObjectUtils.isEmpty(rukuShenqing)) {
            throw new ServiceException("请选择正确的数据");
        }
        List<RukuShenqingDetail> rukuShenqingDetails = rukuShenqingDetailMapper.getByRkId(id);
        RukuShenqingVo rukuShenqingVo = new RukuShenqingVo();
        rukuShenqingVo.setRukuShenqing(rukuShenqing);
        rukuShenqingVo.setRukuShenqingDetails(rukuShenqingDetails);
        return rukuShenqingVo;
    }

    @Override
    @Transactional
    public int edit(RukuShenqingVo rukuShenqingVo) {
        // 申请状态为待审核，已审核不可修改
        if (ObjectUtils.isEmpty(rukuShenqingVo)) return 0;

        RukuShenqing rukuShenqing = rukuShenqingVo.getRukuShenqing();
        List<RukuShenqingDetail> rukuShenqingDetails = rukuShenqingVo.getRukuShenqingDetails();
        Long id = rukuShenqing.getId();

        // 查询数据库，申请状态为待审核，已审核不可修改
        RukuShenqing byId = rukuShenqingMapper.getById(id);
        Integer shenqingStatus = byId.getShenqingStatus();
        if (shenqingStatus == 1 || shenqingStatus == 2) throw new ServiceException("该数据不可修改");

        // 可以修改
        int result = rukuShenqingMapper.edit(rukuShenqing);

        // 清空详细表
        rukuShenqingDetailMapper.delByRkId(Collections.singletonList(id));

        if (ObjectUtils.isEmpty(rukuShenqingDetails)) {
            // 为空直接返回
            return result;
        }

        rukuShenqingDetails.forEach(
                item -> item.setRukuShenqingId(id)
        );

        rukuShenqingDetailMapper.batchSave(rukuShenqingDetails);
        return result;
    }
}
